using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Random = UnityEngine.Random;


/// <summary>
/// 卡牌工厂，负责创建卡牌实例
/// </summary>
public class CardFactory : MonoBehaviour
{
    [Header("配置引用")]
    public CardConfig cardConfig;

    public CardManager cardManager;
    private EffectSystem effectSystem;

    private void Awake()
    {
        if (cardManager == null)
        cardManager = FindObjectOfType<CardManager>();
        
        if (effectSystem == null)
            effectSystem = FindObjectOfType<EffectSystem>();
    }
    

    /// <summary>
    /// 根据ID创建卡牌
    /// </summary>
    public Card CreateCard(string cardId)
    {
        CardConfig.CardData data = cardConfig.GetCardData(cardId);
        if (data == null)
        {
            Debug.LogError($"卡牌ID不存在: {cardId}");
            return null;
        }

        return CreateCardFromData(data);
    }

    /// <summary>
    /// 根据配置数据创建卡牌
    /// </summary>
    private Card CreateCardFromData(CardConfig.CardData data)
    {
        // 这里根据卡牌ID创建具体的卡牌实例
        switch (data.id)
        {
            case "strike":
                return new StrikeCard(cardManager, effectSystem);
            case "defend":
                return new DefendCard(cardManager, effectSystem);
            case "bash":
                return new BashCard(cardManager, effectSystem);
            case "shrug_it_off":
                return new ShrugItOffCard(cardManager, effectSystem);
            case "anger":
                return new AngerCard(cardManager, effectSystem);
            case "fortitude":
                return new FortitudeCard(cardManager, effectSystem);
            default:
                Debug.LogError($"未知的卡牌ID: {data.id}");
                return null;
        }
    }

    /// <summary>
    /// 创建初始牌组
    /// </summary>
    public List<Card> CreateStarterDeck()
    {
        List<Card> starterDeck = new List<Card>();

        // 添加5张打击
        for (int i = 0; i < 5; i++)
        {
            starterDeck.Add(CreateCard("strike"));
        }

        // 添加4张防御
        for (int i = 0; i < 4; i++)
        {
            starterDeck.Add(CreateCard("defend"));
        }

        // 添加1张重击
        starterDeck.Add(CreateCard("bash"));

        return starterDeck;
    }

    /// <summary>
    /// 获取随机卡牌奖励（通常3选1）
    /// </summary>
    public List<Card> GetRandomRewardCards(int count = 3, Rarity maxRarity = Rarity.Common)
    {
        List<Card> rewardCards = new List<Card>();
        List<CardConfig.CardData> availableCards = cardConfig.allCards.FindAll(card => card.rarity <= maxRarity);

        // 随机选择
        for (int i = 0; i < count && availableCards.Count > 0; i++)
        {
            int randomIndex = Random.Range(0, availableCards.Count);
            Card card = CreateCardFromData(availableCards[randomIndex]);
            if (card != null)
            {
                rewardCards.Add(card);
                availableCards.RemoveAt(randomIndex);
            }
        }

        return rewardCards;
    }
}